--[[
--	Fire
--	 	Trial by Fire
--	
--	By: Alexander Brazie, AnduinLothar
--	
--	Fire is a World of Warcraft in-game debugger. Using fire you can 
--	view the status of any variable or expression in a customizable 
--	tree view. 
--	
--	$Id: Fire.lua 22 2010-10-16 01:23:13Z Kharthus1 $
--	$Rev: 22 $
--	$LastChangedBy: Kharthus1 $
--	$Date: 2010-10-16 01:23:13 +0000 (Sat, 16 Oct 2010) $
--]]

-- Data

-- Panes
Fire_Panes = {
	"FireFrameDebugger";
	"FireFrameStatusMonitor";
};

Fire_Pane_Tooltip = {
	"Lua Debugger";
	"System Details";
};

LIMIT = 0;
FIRE_EVAL_MAX_DEPTH = 15;

-- Functions
Fire = {};

--[[ Loads a string into the Gui ]]--
Fire_EvalStringAndLoadTree = function(input)
	RunScript("Fire_GlobalEvalString="..input)
	local t = Fire_GlobalEvalString;
	if ( type(t) ~= "table" ) then
		t = {t};
	end

	local key = FireFrameDebuggerFilterInput:GetText();
	if ( key == nil ) then key = ".*"; end

	-- Make a list of the keys to use
	local tKeys = {};
	for k,v in pairs(t) do
		table.insert(tKeys, k);
	end

	-- Copy data to ensure we don't foobar it
	for k,v in pairs(tKeys) do
		if ( strfind(tostring(v), key ) )then
		else
			tKeys[k] = nil;
		end
	end

	-- Sort the keys
	table.sort(tKeys, function(a,b) return tostring(a) < tostring(b); end);

	-- Count the original
	local tCount = 0;
	for k,v in pairs(t) do
		tCount = tCount+1;
	end
	
	-- Let the user know
	if ( tCount == 0 ) then
		FireTree_LoadEnhanced(FireFrameDebuggerContainerTree, {{ title=FIRE_NILRETURNED, titleColor=GRAY_FONT_COLOR }} );
	elseif ( table.getn(tKeys) == 0 ) then
		FireTree_LoadEnhanced(FireFrameDebuggerContainerTree, {{ title=FIRE_NOMATCH, titleColor=GRAY_FONT_COLOR }} );
	else
		--table.sort(render);
		FireTree_LoadTable(FireFrameDebuggerContainerTree, t, nil, tKeys, FIRE_EVAL_MAX_DEPTH);
	end
	
	--FireTree_UpdateFrame(FireFrameDebuggerContainerTree);
	FireTree_CollapseAllToggle(FireFrameDebuggerContainerTree)
end;

--[[ Loads a string into fire ]]--
function Fire_Debugger_LoadString(string)
	FireFrameDebuggerEvalInput:SetText(string);
	Fire_EvalStringAndLoadTree(FireFrameDebuggerEvalInput:GetText());

	if ( not FireFrame:IsVisible() ) then 
		FireFrame:Show();
	end
end;

--[[ Toggles Fire ]]--
function ToggleFire()
	if ( not FireFrame:IsVisible() ) then
		FireFrame:Show();
	else
		FireFrame:Hide();
	end
end

--[[ Toggles Subpanes ]]--
function ToggleFirePane(id) 
	for k,v in pairs(Fire_Panes) do
		_G[v]:Hide();
	end

	_G[Fire_Panes[id]]:Show();
end

--[[ Quick Eval Jump ]]--
function Fire_QuickEval ()
	if ( not FireFrame:IsVisible() ) then
		FireFrame:Show();
		ToggleFirePane(1); 
		if (Chronos) then
			Chronos.schedule(.04, function() FireFrameDebuggerEvalInput:SetFocus(); end );
		end
	else
		FireFrameDebuggerEvalInput:ClearFocus();
		FireFrame:Hide();
	end
end

--[[ Event Handlers ]]--
function Fire_Pane_OnClick(self)
	local id = self:GetID();	
	ToggleFirePane(id);
end

function Fire_Eval_OnLoad(self)
	self:SetScript("OnEscapePressed", Fire_Eval_OnEscapePressed);
	self:SetScript("OnEnterPressed", Fire_Eval_OnEnterPressed);
	self:SetScript("OnTabPressed", Fire_Eval_OnTabPressed);
end
function Fire_Eval_OnEscapePressed(self)
	self:ClearFocus();
end
function Fire_Eval_OnEnterPressed(self)
	Fire_EvalStringAndLoadTree(self:GetText());
	--self:ClearFocus();
end
function Fire_Eval_OnTabPressed(self)
	FireFrameDebuggerFilterInput:SetFocus();
end

function Fire_EvalButton_OnLoad(self)
	self:SetScript("OnClick", Fire_EvalButton_OnClick);
end
function Fire_EvalButton_OnClick(self)
	Fire_EvalStringAndLoadTree(FireFrameDebuggerEvalInput:GetText());
end

function Fire_Filter_OnLoad(self)
	self:SetScript("OnEscapePressed", Fire_Filter_OnEscapePressed);
	self:SetScript("OnEnterPressed", Fire_Filter_OnEnterPressed);
	self:SetScript("OnTabPressed", Fire_Filter_OnTabPressed);
end
function Fire_Filter_OnEscapePressed(self)
	self:ClearFocus();
end
function Fire_Filter_OnEnterPressed(self)
	Fire_EvalStringAndLoadTree(FireFrameDebuggerEvalInput:GetText());
	--self:ClearFocus();
end
function Fire_Filter_OnTabPressed(self)
	FireFrameDebuggerEvalInput:SetFocus();	
end
function Fire_FilterButton_OnLoad(self)
	self:SetScript("OnClick", Fire_FilterButton_OnClick);
end

function Fire_FilterButton_OnClick(self)
	Fire_EvalStringAndLoadTree(FireFrameDebuggerEvalInput:GetText());
end

function Fire_Tab_OnEnter(self)
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
	GameTooltip:SetText(Fire_Pane_Tooltip[self:GetID()], 1.0,1.0,1.0 );
end
function Fire_Tab_OnLeave(self)
	GameTooltip:Hide();
end

function Fire_Frame_OnLoad(self)
	UIPanelWindows["FireFrame"] = { area = "center",	pushable = 0 };
	
	SlashCmdList[FIRE_EVAL_ID] = Fire_Debugger_LoadString;
	for i=1, #FIRE_EVAL_CMD do _G["SLASH_"..FIRE_EVAL_ID..i] = FIRE_EVAL_CMD[i]; end;

	-- Frame Properties
	self.numTabs = 2;
	self.selectedTab = 1;
	_G[self:GetName().."HeaderText"]:SetText(FIRE_TITLE_TEXT);
	
	--PanelTemplates_SetTab(self, 1);
	FireFrameDebuggerContainerTree.collapseAllArtwork = false;
end
